<template>
  <div class="app-container">
    <!--页头-->
    <div class="filter-container">
      <el-input
        :placeholder="'类别名称'"
        v-model="listQuery.name_LIKE"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="reset" @click="resetCondition">
        <svg-icon icon-class="reset"/>
        重置
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate">添加
      </el-button>
      <el-button
        v-waves
        :loading="downloadLoading"
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        @click="handleDelete">删除
      </el-button>
    </div>

    <!--table主体-->
    <el-table
      v-loading="listLoading"
      ref="table"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column width="33" type="selection" align="center"/>
      <el-table-column :label="'类别名称'" align="center" fixed>
        <template slot-scope="scope">
          <span>{{ scope.row.name+(undefined===scope.row.model?'':' / '+scope.row.model) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'备注'" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"/>

    <!--弹框-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" style="margin-left:8px;">
        <el-form-item label="类别名称" prop="name">
          <el-input v-model="temp.name" clearable/>
        </el-form-item>
        <el-row :gutter="5">
          <el-col :span="24" :xs="24">
            <el-form-item label="备注">
              <el-input v-model="temp.remark" type="textarea" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">{{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, fetchList, modify, remove, see } from '@/api/productCategory.js'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'ProductList',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null, // 主体数据
      total: 0, // 总条数
      listLoading: true, // 是否加载中
      listQuery: { // 查询条件
        page: 1,
        limit: 10,
        sort: undefined
      },
      uploadUrl: process.env.BASE_API + '/file/upload',
      temp: {},
      dialogFormVisible: false,

      dialogStatus: '', // 当前弹窗
      textMap: { //  弹窗类型
        update: '修改产品',
        create: '添加产品'
      },
      rules: {
        name: [{ required: true, message: '类别名称不能为空', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 重置搜索框
    resetCondition() {
      this.listQuery.name_LIKE = ''
      this.getList()
    },
    // 删除
    handleDelete() {
      var ids = ''
      this.$refs.table.selection.forEach(function(row) {
        if (ids !== '') {
          ids += ','
        }
        ids += row.id
      })
      if (ids === '') {
        this.$message({
          type: 'warning',
          message: '请选择要删除的数据!'
        })
        return
      }
      this.$confirm('删除后不可恢复，是否确认删除?', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(ids).then((response) => {
          if (response.success) {
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          } else {
            this.$message.error('删除失败!')
          }
        })
      })
    },
    // 获得数据
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.rows
        this.total = response.total
        this.listLoading = false
      })
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 创建页面
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 创建
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          add(this.temp).then((response) => {
            if (response.success) {
              this.$message({
                type: 'success',
                message: '添加成功!'
              })
              this.dialogFormVisible = false
              this.getList()
            }
          })
        } else {
          console.log('验证不通过')
        }
      })
    },
    // 更新页面
    handleUpdate(row) {
      this.listLoading = true
      this.dialogStatus = 'update'

      see(row.id).then(response => {
        this.dialogFormVisible = true
        this.temp = response.data
        this.listLoading = false
      })
    },
    // 更新数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          modify(this.temp).then((response) => {
            if (response.success) {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.dialogFormVisible = false
              this.getList()
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped>
  img {
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
  }
</style>
